﻿''' <summary>
''' Provides a text label for the game's heads-up-display (HUD).
''' </summary>
''' <remarks></remarks>
Public Class HudLabel
    Inherits GameObject

    Private _LabelBounds As Rectangle
    ''' <summary>
    ''' Gets the calculated on-screen rectangle bounding this label.
    ''' </summary>
    ''' <value></value>
    ''' <returns>The calculated on-screen rectangle bounding this label.</returns>
    ''' <remarks></remarks>
    Public ReadOnly Property ActualBounds As Rectangle
        Get
            Return _LabelBounds
        End Get
    End Property

    ''' <summary>
    ''' Gets the calculated size of the label based on text and rendering options.
    ''' </summary>
    ''' <value></value>
    ''' <returns>The actual size of the label.</returns>
    ''' <remarks></remarks>
    Public ReadOnly Property ActualSize As Size
        Get
            Return _LabelBounds.Size
        End Get
    End Property

    ''' <summary>
    ''' Gets the calculated position of the label if UseCenterPositioning is specified, otherwise gets Position.
    ''' </summary>
    ''' <value></value>
    ''' <returns>The on-screen position of the label.</returns>
    ''' <remarks></remarks>
    Public ReadOnly Property ActualPosition As Point
        Get
            Return _LabelBounds.Location
        End Get
    End Property

    ''' <summary>
    ''' Specifies whether the label should be sized automaticaly according to its text.
    ''' </summary>
    ''' <value></value>
    ''' <returns>True if the label is sized automatically, otherwise false.</returns>
    ''' <remarks></remarks>
    Public Property AutoSize As Boolean = True
    Public Property ProposedSize As Size
    Public Property BackColor As Color = Color.Transparent
    Public Property TextColor As Color = Color.White
    Public Property TextFormat As TextFormatFlags = TextFormatFlags.HorizontalCenter
    Public Property Text As String
    Public Property UseCenterPositioning As Boolean = True

    Private _FontName As String
    Public Property FontName As String
        Get
            Return _FontName
        End Get
        Set(value As String)
            If value Is Nothing OrElse value.Length = 0 Then
                value = "Segoe UI"
            End If
            If Not _FontName = value Then
                _FontName = value
                ResetFont()
            End If
        End Set
    End Property

    Private _FontSize As Integer = 10
    Public Property FontSize As Integer
        Get
            Return _FontSize
        End Get
        Set(value As Integer)
            If value < 1 OrElse value > 256 Then
                value = 10
            End If
            If Not _FontSize = value Then
                _FontSize = value
                ResetFont()
            End If
        End Set
    End Property

    Private _FontStyle As FontStyle
    Public Property FontStyle As FontStyle
        Get
            Return _FontStyle
        End Get
        Set(value As FontStyle)
            If Not _FontStyle = value Then
                _FontStyle = value
                ResetFont()
            End If
        End Set
    End Property

    Private _Font As Font

    Public Sub New()
        _ZOrder = 1
        FontName = "Segoe UI"
        FontSize = 10
        FontStyle = Drawing.FontStyle.Regular
        ResetFont()
        _TextColor = Color.White
        _TextFormat = TextFormatFlags.HorizontalCenter Or TextFormatFlags.VerticalCenter
        IsCollider = False
        ProcessInput = False
    End Sub

    Private Sub ResetFont()
        If _Font IsNot Nothing Then _Font.Dispose()
        _Font = New Font(FontName, FontSize, FontStyle)
    End Sub

    Public Overrides Function GetRectangleCollider() As System.Drawing.RectangleF
        Return _LabelBounds
    End Function

    Protected Friend Overrides Sub OnDraw(ByVal e As GdiGaming.DrawEventArgs)
        MyBase.OnDraw(e)
        If _Text IsNot Nothing AndAlso _Text.Length > 0 Then
            Dim backBrush As New SolidBrush(_BackColor)
            e.PaintEventArgs.Graphics.FillRectangle(backBrush, _LabelBounds)
            'TextRenderer.DrawText(e.PaintEventArgs.Graphics, _Text, _Font, _LabelBounds, _TextColor, TextFormat)
            Dim textBrush As New SolidBrush(_TextColor)
            e.PaintEventArgs.Graphics.DrawString(_Text, _Font, textBrush, _LabelBounds)
            textBrush.Dispose()
            backBrush.Dispose()
        End If
    End Sub

    Protected Friend Overrides Sub OnUpdate(ByVal e As GdiGaming.GameEngineEventArgs)
        MyBase.OnUpdate(e)
        If _Text IsNot Nothing AndAlso _Text.Length > 0 Then
            Dim labelSize As Size
            If _AutoSize Then
                labelSize = TextRenderer.MeasureText(_Text, _Font, ProposedSize, TextFormat)
            Else
                labelSize = ProposedSize
            End If

            If UseCenterPositioning Then
                _LabelBounds = New Rectangle(e.Engine.Canvas.DisplayRectangle.Width / 2 - labelSize.Width / 2 + Position.X,
                                             e.Engine.Canvas.DisplayRectangle.Height / 2 - labelSize.Height / 2 + Position.Y,
                                             labelSize.Width, labelSize.Height)
            Else
                _LabelBounds = New Rectangle(Position, labelSize)
            End If
        Else
            _LabelBounds = New Rectangle(Position, Size.Empty)
        End If
    End Sub

End Class
